#!/usr/bin/env python

import psycopg2
import matplotlib
matplotlib.use('Agg')
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 
plt.style.use('ggplot')

def initDatabase():
    return psycopg2.connect("dbname=trending_topic user=atropos host='/var/run/postgresql/'") 

def geneTopicNumStatus():
    """
    Generate number of readings per trending topic per day
    on sina.
    """
    db_conn = initDatabase()
    db = db_conn.cursor()
    topic_statis = dict()
    for rank in range(1, 31):
        num_readings = dict()  
        # for each day, retrieve some topics.
        db.execute("select id, num_readings, num_discussion, num_fans, timestamp " + \
                "from topic_statis where rank = %s order by timestamp", (rank,))
        topics = cleanNumReadings(db.fetchall())
        index = 0
        num_readings[topics[0][-1]] = topics[1][1] - topics[0][1]
        for index in range(1, len(topics)-1):
            if topics[index][0] != topics[index+1][0]:
                num_readings[topics[index][-1]] = num_readings[topics[index-1][-1]]
            else:
                num_readings[topics[index][-1]] = topics[index+1][1] - topics[index][1] 
            index += 1
        #print(rank)
        #print(num_readings)
        topic_statis[rank] = numrsPerDay(num_readings)
    db.close()
    db_conn.close()
    return topic_statis
def numrsPerDay(statis):
    num_readings = dict()
    index = 0
    for ts, num in statis.items():
        ts = ts[:-3]
        if ts in num_readings:
            num_readings[ts] += num
        else:
            num_readings[ts] = num
    return num_readings



def cleanNumReadings(topics):
    last_end = 0
    last_begin = -1
    while True:
        if int(topics[last_end][-1][-3:]) == 0:
            break;
        last_end -= 1
    last_begin = 0
    while True:
        if int(topics[last_begin][-1][-3:]) == 0:
            break;
        last_begin += 1
    topics = topics[last_begin:last_end+1]
    index = 0
    #print((int(topics[-1][-1][:-3]) - int(topics[0][-1][:-3]))*144 + 1)
    for i in range(0, (int(topics[-1][-1][:-3]) - int(topics[0][-1][:-3]))*144 + 1):
        #print(i)
        ts = topics[i][-1][-3:]
        if int(ts) != index:
            topics.insert(i, topics[i-1])
            topics[i] = list(topics[i])
            topics[i][-1] = str(int(topics[i][-1]) + 1) 
        if topics[i][1] == 0:
            topics[i] = list(topics[i])
            if topics[i][0] == topics[i-1][0]:
                topics[i][1] = topics[i-1][1]
            elif topics[i][0] == topics[i+1][0]:
                topics[i][1] = topics[i+1][1]
            else:
                topics[i] = topics[i-1]
        index += 1
        if index == 144:
            index = 0
    return topics
def genTopicNumGraph():
    """
    Generate the graph of number of readings along with time.
    """
    num_readings = geneTopicNumStatus();
    topics = []
    sorted_keys = sorted(num_readings.keys())
    array = []
    start_date = ''
    end_date = ''
    for k in sorted_keys:
        topics.append('Top' + str(k))
        col = []
        sorted_ts_keys = sorted(num_readings[k].keys())
        for m in sorted_ts_keys:
            col.append(num_readings[k][m])
        start_date = sorted_ts_keys[0]
        end_date = sorted_ts_keys[-1]
        array.append(col)
    #print(np.array(array).transpose())
    array = np.array(array, dtype=object).transpose()
    #print(array)
    total_per_day = [sum(day) for day in array]
    avg = sum(total_per_day)/len(total_per_day)

    print("Number of Readings per day on the whole Weibo: %s, takes account for %s days data" % (int(avg), len(total_per_day))) 
    print()
    print("%s ~ %s" % (start_date, end_date))
    for i in range(0,len(array)):
        array[i] = [reads_per_topic/total_per_day[i] for reads_per_topic in array[i]]
    print(array)

    df = pd.DataFrame(array, index = pd.date_range(start_date, end_date), columns = topics)
    plt.figure()
    ax = df.plot()
    ax.get_figure().savefig('out.png')

        



def timestamp(month, day):
    ts = '2016'
    if month < 10:
        ts += '0' + str(month)
    else:
        ts += str(month)
    if day < 10:
        ts += '0' + str(day)
    else:
        ts += str(day)
    return int(ts)


if __name__=='__main__':
    genTopicNumGraph()

    
